package cn.tedu.v3_1;

import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;


public class Server {
    private static final Integer PORT=3333;
    private ServerSocket serverSocket;
    private Socket socket;
    private String ip;
    private BufferedReader br;
    private PrintWriter pw;

    public Server() {
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (BindException e) {
            System.out.println("请检查端口号，被占用");
        } catch (IOException e) {
            System.out.println("客户端未进入");
        }
    }

    public void start() {

        try {
            while (true) {
                System.out.println("——————欢迎进入聊天室—————");

                System.out.println("等待客户端链接");
                socket = serverSocket.accept();
                ip=socket.getInetAddress().getHostAddress();
                new Thread(new ClientHandler(socket)).start();
                System.out.println(ip+"客户端连接成功");
            }
            } catch(IOException e){
                System.out.println("客户端" + ip + "连接失效了");
            } finally{
            if(serverSocket!=null&&!serverSocket.isClosed()){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    System.out.println("服务器关闭失败");
                }
                System.out.println("服务器关闭成功");
            }
            }
        }
    public static void main(String[] args) {
        Server server = new Server();
        server.start();


    }
    private class ClientHandler implements Runnable {
        private Socket socket;
        private String ip;
        public  ClientHandler(Socket s) {
            this.socket = s;
            ip = socket.getInetAddress().getHostAddress();
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));

                pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
            } catch (IOException e) {
                System.out.println("客户端断开连接");
            }


        }

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        public void run() {


          try {

              new Thread(()->{
                try {
                  String str;
                  while ((str=br.readLine())!=null) {
                      System.out.println(sdf);
                  System.out.println(ip + "客户端： " + str);

                    }
                } catch (IOException e) {
                        System.out.println("客户端断开了");
                }
         }).start();
                System.out.println(ip+"服务器端用户：请输入文字");
              Scanner input=new Scanner(System.in);
                while(true){
                    String say = input.nextLine();
                    pw.println("服务器端说"+say);
                    System.out.println("发送");
                    System.out.println("服务器端请输入： ");


                }


            }  finally {
                if(pw!=null)
                    pw.close();
                if(br!=null)
                    try {
                        br.close();
                    } catch (IOException e) {
                        System.out.println("服务器端读入异常");
                    }
                if(socket!=null&&!socket.isClosed()){
                    System.out.println(ip+"客户端退出服务器连接");
                    try {
                        socket.close();
                    } catch (IOException e) {
                        System.out.println(ip+"客户端退出链接异常");
                    }
                }
            }
        }
    }
        }